package algorithm.stack;

import algorithm.array.MyArray;

/**
 * @author bingo
 * @Description  栈的实现
 * @Date 2018/6/1
 */
public class ArrayStack<E> implements  Stack<E> {

    private MyArray<E> myArray;

    public ArrayStack(int capacity) {
        myArray = new MyArray(10);
    }

    @Override
    public void push(E e) {

        myArray.insertLast(e);
    }

    @Override
    public E pop() {

        return  myArray.removeLast();
    }

    @Override
    public E peek() {
       return  myArray.getLast();
    }

    @Override
    public int getSize() {
        return myArray.getSize();
    }

    @Override
    public boolean isEmpty() {
        return myArray.isEmpty();
    }
    @Override
    public String toString(){
        StringBuilder result = new StringBuilder("Stack[");
        for(int i=0;i<myArray.getSize();i++){
            result.append(myArray.get(i));
            if(i<myArray.getSize()-1){
                result.append(",");
            }
        }
        result.append("]");
        return  result.toString();

    }

    public static void main(String[] args) {

        ArrayStack arrayStack = new ArrayStack(10);
        arrayStack.push(1);

        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.push(4);
        arrayStack.push(5);
        System.out.println(arrayStack.toString());
        arrayStack.pop();
        System.out.println(arrayStack.toString());
        arrayStack.pop();
        System.out.println(arrayStack.toString());
        System.out.println(arrayStack.peek());


    }
}



//    {[()]}
